// pages/login/login.js

//const utils = require('../../utils/util')
const http=require('../../utils/http')

Page({

  /**
   * Page initial data
   */
  data: {
      account:"15989541735",
      password:"541735",
      pw_visable:false
  },

  bindAccountInput: function (e) {
    this.setData({
      account: e.detail.value
    })
  },

  bindPasswordInput:function(e){
     this.setData({
       password:e.detail.value
     })
  },

  clickPwVisable(){
    this.setData({
      pw_visable:!this.data.pw_visable
    })
  },

  goChangePw(){
     wx.navigateTo({
       url: '../changepw/changepw',
     })
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
   
  },


   login(){

    if(this.data.account.length==0){
       wx.showToast({
         title: '账号不能为空',
         icon:'none'
       })
       return
    }

    if(this.data.password.length==0){
      wx.showToast({
        title: '密码不能为空',
        icon:'none'
      })
      return
   }

      wx.showLoading({
      title: '请稍等...',
     // mask:true
     })

     var params= {
          user:this.data.account,
          password:this.data.password
      }
      http.postData('api/sys/login',params).then((value)=>{
          if(value['code']==0){
            getApp().globalData.token=value.data['token']
            getApp().globalData.phone=value.data['staff_mobile']
            getApp().globalData.name=value.data['staff_name']
            getApp().globalData.staff_duty=value.data['staff_duty']
            getApp().globalData.staff_org=value.data['staff_org']
            getApp().globalData.staff_number=value.data['staff_number']
            this.getPermissions()
          }else{
            wx.hideLoading()
          }
      }).catch((reason)=>{
            wx.hideLoading()
            wx.showToast({
              title: reason['msg'],
              icon:'none'
            })
      })

    },

    getPermissions() {
         let permissions=[]
         permissions.push("api/pxs/appbss/customer/all");
         permissions.push("api/pxs/appbss/customer/appfollowList");
         permissions.push("api/pxs/appbss/customer_pool/all");
         permissions.push("api/pxs/appbss/find_shop/all");
         permissions.push("api/pxs/appbss/group_level/levelAll");
         permissions.push("api/pxs/appbss/group_level/levelCheck");
         permissions.push("api/pxs/appbss/group_level/levelList");
         permissions.push("api/pxs/appbss/bv/overall");
         permissions.push("api/pxs/appbss/bv/company_staff");
         permissions.push("api/pxs/appbss/customer/customer_signed_change");
         permissions.push("api/pxs/appbss/new_find_shop/all");
         permissions.push("api/pxs/appbss/finance/ts/list");

         var params2={
           permission:permissions
         }

        http.postData('api/sys/user/info',params2)
        .then((value)=>{
           wx.hideLoading()
           if(value['code']==0){
            let list=Array.from(value['data']['permission'])
            list.forEach(value => {
              if (value=='api/pxs/appbss/bv/company_staff') {
                if (getApp().globalData.ROLE<1) {
                  getApp().globalData.ROLE=1
                }
              }else if (value=='api/pxs/appbss/bv/overall') {
                getApp().globalData.ROLE=2
              }else if (value==='api/pxs/appbss/new_find_shop/all') {
                getApp().globalData.findshop_permission.push({icon:'../../images/icon_fs',title:"找店客户"})
              }else if (value==="api/pxs/appbss/finance/ts/list") {
                getApp().globalData.finance_permission.push({icon:'../../images/h_b_8_blue',title:"转店收退款"})
              }else{
                if (value=='api/pxs/appbss/customer/all') {
                  getApp().globalData.transfer_permission.push({icon:'../../images/h_b_1',title:"客户列表"})
                }else if (value=='api/pxs/appbss/customer/appfollowList'){
                  getApp().globalData.transfer_permission.push({icon:'../../images/h_b_2',title:"客户跟进"})
                }else if (value=='api/pxs/appbss/customer_pool/all'){
                  getApp().globalData.transfer_permission.push({icon:'../../images/h_b_3',title:"资源池"})
                }
              }
            });
            getApp().globalData.staff_picture=value.data['staff_picture']
            getApp().globalData.branch_id=value.data['branch_id']
            if (value.data['bi_value']==2) {
              getApp().globalData.UserInviteCode=value.data['qr_code']
            }

            wx.showToast({
              title: '登录成功',
              icon:'none'
            }).then((value)=>{
              wx.redirectTo({
                url: '../main/Main',
              })
            })

           }
        })
        .catch((error)=>{
          wx.hideLoading()
        })
   },
  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})